package com.admin.component.declare;

import com.admin.service.declare.BaseDeclareService;
import com.admin.service.sample.SampleService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author weiyang
 * @date 2022/5/25
 */
@Component
public class DeclareHandlerDispatcher implements ApplicationContextAware {

    private Map<String, BaseDeclareService> declareServiceMap;

    public BaseDeclareService getHandler(String type) {
        return declareServiceMap.get(type);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, BaseDeclareService> beansOfType = applicationContext.getBeansOfType(BaseDeclareService.class);
        declareServiceMap = beansOfType.values().stream().collect(Collectors.toMap(e->e.getType().name(), v -> v, (k1, k2) -> k1));
    }

}
